//
//  SubView1ForWaringInformation.m
//  HondaPro_2
//
//  Created by Eric on 13-6-8.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "SubView1ForWaringInformation.h"
#import "MainMenuDataModel.h"
#import "UserDefinedCellForWaringInformationOfWaringInformation.h"

@interface SubView1ForWaringInformation ()

//创建控件
-(void)createLabels;
-(void)createTableView;

//刷新控件内容
-(void)freshenViewDis;

@end

@implementation SubView1ForWaringInformation

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        [self createLabels];
        [self createTableView];
    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    remarksLabel.text = @"注意：该车因产品品质问题累计保修天数0天";
    //[self freshenViewDis];
}

#pragma mark - 创建控件

-(void)createLabels
{
    NSArray *arrForStaticLabelKeys =
    [NSArray arrayWithObjects:
     @"yujingxinxi",
     @"LabelBottom",
     MM_WARING_INFORMATION_WARING_INFORMATION_LIST_WARING_INFORMATION_PART_CODE_KEY_STR_,
     MM_WARING_INFORMATION_WARING_INFORMATION_LIST_WARING_INFORMATION_PART_NAME_KEY_STR_,
     MM_WARING_INFORMATION_WARING_INFORMATION_LIST_WARING_INFORMATION_ADD_UP_REPAIR_QTY_KEY_STR_, nil];
    
    NSDictionary *dicForStaticLabelsText =
    [NSDictionary dictionaryWithObjectsAndKeys:
     @"预警信息汇总",@"yujingxinxi",
     @"",@"LabelBottom",
     @"零件前五位",MM_WARING_INFORMATION_WARING_INFORMATION_LIST_WARING_INFORMATION_PART_CODE_KEY_STR_,
     @"零件名",MM_WARING_INFORMATION_WARING_INFORMATION_LIST_WARING_INFORMATION_PART_NAME_KEY_STR_,
     @"截止目前累计修理次数",MM_WARING_INFORMATION_WARING_INFORMATION_LIST_WARING_INFORMATION_ADD_UP_REPAIR_QTY_KEY_STR_,nil];
    
    NSDictionary *dicForStaticLabelsRect =
    [NSDictionary dictionaryWithObjectsAndKeys:
     [NSValue valueWithCGRect:CGRectMake(1, 1, 100, 30)],@"yujingxinxi",
     [NSValue valueWithCGRect:CGRectMake(0, 35, self.frame.size.width, 30)],@"LabelBottom",
     [NSValue valueWithCGRect:CGRectMake(0, 35, 160, 29)],MM_WARING_INFORMATION_WARING_INFORMATION_LIST_WARING_INFORMATION_PART_CODE_KEY_STR_,
     [NSValue valueWithCGRect:CGRectMake(161, 35, 160, 29)],MM_WARING_INFORMATION_WARING_INFORMATION_LIST_WARING_INFORMATION_PART_NAME_KEY_STR_,
     [NSValue valueWithCGRect:CGRectMake(322, 35, 176, 29)],MM_WARING_INFORMATION_WARING_INFORMATION_LIST_WARING_INFORMATION_ADD_UP_REPAIR_QTY_KEY_STR_,nil];
    
    NSMutableDictionary *dicForStaticLabels = [NSMutableDictionary dictionaryWithCapacity:5];
    
    for (int i = 0; i<[arrForStaticLabelKeys count]; i++)
    {
        UILabel *lbl = [[UILabel alloc] initWithFrame:[[dicForStaticLabelsRect objectForKey:[arrForStaticLabelKeys objectAtIndex:i]] CGRectValue]];
        lbl.backgroundColor = [UIColor clearColor];
        lbl.font = [UIFont systemFontOfSize:15];
        lbl.text = [dicForStaticLabelsText objectForKey:[arrForStaticLabelKeys objectAtIndex:i]];
        lbl.textColor = [UIColor grayColor];
        [self addSubview:lbl];
        
        [dicForStaticLabels setObject:lbl forKey:[arrForStaticLabelKeys objectAtIndex:i]];
    }
    
    ((UILabel *)[dicForStaticLabels objectForKey:@"LabelBottom"]).backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    
    UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 65, self.frame.size.width, 203)];
    lbl.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    lbl.font = [UIFont systemFontOfSize:15];
    lbl.textColor = [UIColor grayColor];
    [self addSubview:lbl];
    
    remarksLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 268, self.frame.size.width, 30)];
    remarksLabel.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
    remarksLabel.font = [UIFont systemFontOfSize:13];
    remarksLabel.textColor = [UIColor grayColor];
    [self addSubview:remarksLabel];
}

-(void)createTableView
{
    tableViewDis = [[UITableView alloc] initWithFrame:CGRectMake(0, 65, self.frame.size.width, 202)];
    
    tableViewDis.dataSource = self;
    tableViewDis.delegate = self;
    
    tableViewDis.backgroundColor = [UIColor clearColor];
    
    [self addSubview:tableViewDis];
}

#pragma mark - 刷新控件内容

-(void)freshenViewDis
{
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    
    //更新label数据
    if ([modelIns.waringInformation count]>0) {
        NSString *strTmp = [[modelIns.waringInformation objectAtIndex:0] objectForKey:MM_WARING_INFORMATION_WARING_INFORMATION_LIST_ADD_UP_WARRANTY_DAYS_KEY_STR_];
        remarksLabel.text = [NSString stringWithFormat:@"注意：该车因产品品质问题累计保修天数%@天",strTmp];
        
        [tableViewDis reloadData];
    }else{
        UIAlertView *alertView= [[UIAlertView alloc] initWithTitle:@"该车辆不在保修范围内！" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定",nil];
        [alertView show];
    }
    
}

#pragma mark - tableView DataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
//    NSARR *dic = modelIns.waringInformation;
//    NSArray *arr = [dic objectForKey:MM_WARING_INFORMATION_WARING_INFORMATION_LIST_KEY_STR_];
    NSArray *arr = modelIns.waringInformation;
    return 4+[arr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    UserDefinedCellForWaringInformationOfWaringInformation *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (nil == cell)
    {
        cell = [[UserDefinedCellForWaringInformationOfWaringInformation alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    NSInteger row = [indexPath row];
    
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
//    NSDictionary *dic = modelIns.waringInformation;
//    NSArray *arr = [dic objectForKey:MM_WARING_INFORMATION_WARING_INFORMATION_LIST_KEY_STR_];
    NSArray *arr = modelIns.waringInformation;
    
    //cell的风格是：浅浅灰色和浅灰色间隔出现，第一行是浅灰色
    switch (row%2)
    {
        case 0:
            //浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
            break;
        case 1:
            //浅浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1];
            break;
            
        default:
            break;
    }
    
    if (row<[arr count])
    {
        for (int i = 0; i<[cell.arrForKeys count]; i++)
        {
            ((UILabel *)[cell.dicForLabelsOnCell objectForKey:[cell.arrForKeys objectAtIndex:i]]).text =
            [NSString stringWithFormat:@"%@",[[arr objectAtIndex:row] objectForKey:[cell.arrForKeys objectAtIndex:i]]];
        }
    }
    else
    {
        for (int i = 0; i<[cell.arrForKeys count]; i++)
        {
            ((UILabel *)[cell.dicForLabelsOnCell objectForKey:[cell.arrForKeys objectAtIndex:i]]).text =@"";
        }
    }
    
    return cell;
}

#pragma mark - tableView Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

@end
